#include <stdio.h>

// Info
	// command:
	// move a onto b: return the blocks above a and b to their initial position, then move a to b
	// pile a onto b: return the blocks above b to their initial position, then move the whole part of stack above a to b
	// move a over over b: return the blocks on top of a, and then move a to the top of the stack containing b
	// pile a over b: simply pile one whole block to another
	// quit: for quit

	// illegal command: when a==b

// Data Structure:
	// 1. I'll use pos[25] to store the position of each stack, and element[25][25] to store the current situation
	// 2. a = 25 means unoccupied (n<=25 => 0<=a<=24)

int prompt_for_command(int cmd[4]){
	char s1[10],s2[10];
	scanf("%s",s1);
	if(!strcmp(s1,"quit"))	return 0;

	scanf("%d%s%d",cmd+2,s2,cmd+3);
	if(!strcmp(s1,"move"))	cmd[0]=1;
	else if(!strcmp(s1,"pile")) cmd[0]=2;
	else	{cmd[2]=cmd[3]=0;};
	if(!strcmp(s2,"onto"))	cmd[1]=1;
	else if(!strcmp(s2,"onto"))	cmd[1]=2;
	else {cmd[2]=cmd[3]=0;}
}

void stack_return(int a,int pos1[25],int pos2[25],int ele[25][25], int n){
	int i;
	for(i=pos2[a]+1;ele[pos1[a]][i]!=25;i++)
	{
		pos1[i]=ele[pos1[a]][i];	// return to position i
		pos2[i]=0;
		ele[pos1[a]][i]=25;
	}
}

int main(){
	// pos1: first index in ele of this element; pos2: second index in ele of this element;
	// ele: element; ele is initialized to 0 for all its elements

	int pos1[25],pos2[25],ele[25][25]={};
	int cmd[4];	// pos0: 1: move, 2: pile or 0: quit	pos1: 1: onto or 2: over	pos2: a, pos3: 2
	int i,j;

	int n;	// num of blocks in the block world, 0<n<25


	scanf("%d",&n);
	for(i=0;i<n;i++){pos1[i]=i;pos2[i]=0;ele[i][0]=i;};

	while(prompt_for_command(cmd)){
	// start to operate
		// check legal, if a, b are from the same stack || they are the same
		if(pos1[cmd[2]]==pos2[cmd[3]])	continue;

		// deal with move
		if(cmd[0]==1){
			stack_return(cmd[2],pos1,pos2,ele,n);
			if(cmd[1]==1)	// onto
			{
				stack_return(cmd[3],pos1,pos2,ele,n);
				pos1[cmd[2]]=pos1[cmd[3]];
				pos2[cmd[2]]=pos2[cmd[3]]+1;
				ele[pos1[cmd[2]]][pos2[cmd[2]]]=cmd[2];
			}
			else if(cmd[1]==2)	// over
			{

			}
		}
		// deal with pile

	}

	getchar();
	return 0;
}
